package com.boot.security.server.controller.mffl.mffllawyerletter;

import com.boot.security.server.dao.mffl.MfflLawyerletterDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflCourierServicesCompany;
import com.boot.security.server.model.mffl.MfflCourierServicesOrder;
import com.boot.security.server.model.mffl.MfflLawyerletter;
import com.boot.security.server.model.mffl.Vo.MfflLawyerletterVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.MfflLawyerletter.MfflLawyerletterService;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.boot.security.server.service.impl.mffl.CourierServicesCompany.CourierServicesCompanyService;

import java.util.HashMap;
import java.util.List;

@Api(tags = "律师函（律师端）")
@RestController
@RequestMapping("Lawyer/mfflLawyerletters")
public class Lawyer_MfflLawyerletterController {

    @Autowired
    private MfflLawyerletterDao mfflLawyerletterDao;

    @Autowired
    MfflLawyerletterService mffllawyerletterservice;

    //快递公司
    @Autowired
    CourierServicesCompanyService CourierServicesCompanyService;

    @PostMapping(value = "sendMail")
    @ApiOperation("律师发送邮件")
    public ReturnBean sendMail(@ApiParam(name = "id",value = "律师函id",required = true) @RequestParam Integer id,
                               @ApiParam(name = "courierId",value = "快递id（访问快递接口获取id）" ,required = true) @RequestParam Integer courierId,
                               @ApiParam(name = "courierNumber",value = "快递单号" ,required = true) @RequestParam String courierNumber,
                               @ApiParam(name = "Sender",value = "发件人姓名" ,required = true) @RequestParam String Sender,
                               @ApiParam(name = "originatingPlace",value = "发件人地址" ,required = true) @RequestParam String originatingPlace,
                               @ApiParam(name = "SenderPhone",value = "发件人电话" ,required = true) @RequestParam String SenderPhone){
        if(id == null){
            return new ReturnBean("201","id不能为空");
        }if(courierId == null){
            return new ReturnBean("201","courierId不能为空");
        }if(StringUtils.isEmpty(courierNumber)){
            return new ReturnBean("201","courierNumber不能为空");
        }if(StringUtils.isEmpty(Sender)){
            return new ReturnBean("201","Sender不能为空");
        }if(StringUtils.isEmpty(originatingPlace)){
            return new ReturnBean("201","originatingPlace不能为空");
        }if(StringUtils.isEmpty(SenderPhone)){
            return new ReturnBean("201","SenderPhone不能为空");
        }
        return mffllawyerletterservice.LawyerSendMail(id,courierId,courierNumber,Sender,originatingPlace,SenderPhone);
    }

    @PutMapping(value = "updateMail")
    @ApiOperation("律师修改邮件")
    public ReturnBean updateMail(@ApiParam(name = "id",value = "律师函id",required = true) @RequestParam Integer id,
                                 @ApiParam(name = "sendingid",value = "快递id" ,required = true) @RequestParam Integer sendingid,
                                 @ApiParam(name = "courierId",value = "快递公司id（访问快递接口获取id）" ,required = true) @RequestParam Integer courierId,
                                 @ApiParam(name = "courierNumber",value = "快递单号" ,required = true) @RequestParam String courierNumber,
                                 @ApiParam(name = "Sender",value = "发件人姓名" ,required = true) @RequestParam String Sender,
                                 @ApiParam(name = "originatingPlace",value = "发件人地址" ,required = true) @RequestParam String originatingPlace,
                                 @ApiParam(name = "SenderPhone",value = "发件人电话" ,required = true) @RequestParam String SenderPhone){
        if(id == null){
            return new ReturnBean("201","id不能为空");
        }if(courierId == null){
            return new ReturnBean("201","courierId不能为空");
        }if(sendingid == null){
            return new ReturnBean("201","快递id不能为空");
        }if(StringUtils.isEmpty(courierNumber)){
            return new ReturnBean("201","courierNumber不能为空");
        }if(StringUtils.isEmpty(Sender)){
            return new ReturnBean("201","Sender不能为空");
        }if(StringUtils.isEmpty(originatingPlace)){
            return new ReturnBean("201","originatingPlace不能为空");
        }if(StringUtils.isEmpty(SenderPhone)){
            return new ReturnBean("201","SenderPhone不能为空");
        }
        return mffllawyerletterservice.LawyerUpdateMail(id,sendingid,courierId,courierNumber,Sender,originatingPlace,SenderPhone);
    }



    @PostMapping(value = "Phone")
    @ApiOperation("律师打用户电话")
    public ReturnBean Phone(@ApiParam(name = "id",value = "订单id",required = true)@RequestParam Integer id){
        if (id == null){
            return new ReturnBean("201","订单id");
        }
        return mffllawyerletterservice.Phone(0,id);
    }


    @GetMapping(value = "showcourierbyid")
    @ApiOperation("按律师发送的快递id查询快递信息")
    public ReturnBean<MfflCourierServicesOrder> showCourierbyid(@ApiParam(name = "id",value = "律师函id",required = true)@RequestParam Integer id,
                                                                @ApiParam(name = "courierid",value = "快递id",required = true)@RequestParam Integer courierid){
        if (id == null){
            return new ReturnBean("201","律师函id不能为空");
        }if (courierid == null){
            return new ReturnBean("201","快递id不能为空");
        }
        return mffllawyerletterservice.showCourierbyid(id,courierid);
    }



    @GetMapping(value = "showCourier")
    @ApiOperation("查看律师函快递快递信息")
    public ReturnBean<MfflCourierServicesOrder> showCourier(@ApiParam(name = "id",value = "律师函id",required = true)@RequestParam Integer id){
        if (id == null){
            return new ReturnBean("201","律师函id不能为空");
        }
        return mffllawyerletterservice.showCourier(id);
    }


    @GetMapping(value = "seeCourierServicesCompany")
    @ApiOperation("查看快递公司")
    public List<MfflCourierServicesCompany>  seeCourierServicesCompany(){
        return CourierServicesCompanyService.getCourierServicesCompanyAll();
    }



    @GetMapping
    @ApiOperation(value = "列表")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int")})
    public ReturnBean<MfflLawyerletter> list(PageTableRequest request) {
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset不能为空");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit不能为空");
        }

        //查询当前用户的
        SysUser sysUser = UserUtil.getLoginUser();
        HashMap<String, Object> map = new HashMap<>();
        map.put("serviceLawyerId",sysUser.getId());
        map.put("serviceId", request.getParams().get("serviceId"));
        map.put("delete_state", 1);
        request.setParams(map);

        PageTableResponse handle = new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflLawyerletterDao.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<MfflLawyerletter> list(PageTableRequest request) {
                return mfflLawyerletterDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);

        List<MfflLawyerletter> data = (List<MfflLawyerletter>)handle.getData();
        //不显示未支付状态的订单
        for (int i=0;i<data.size();i++){
            if(data.get(i).getPayState() == 0){
                data.remove(i);
            }
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(handle);
        return returnBean;
    }


    @GetMapping(value = "refuse")
    @ApiOperation(value="律师拒绝接受律师函服务")
    public ReturnBean refuse(@ApiParam(name = "id",value = "律师函id",required = true)@RequestParam Integer id) throws Exception {
        if(id == null){
            return new ReturnBean("201","律师函id不能为空");
        }
        return mffllawyerletterservice.refuse(id);
    }

    @PostMapping(value = "confirm")
    @ApiOperation(value = "律师确认接单")
    public ReturnBean confirm(@ApiParam(value = "合同id",name = "id",required = true)@RequestParam Integer id){
        if(id == null){
            return new ReturnBean("201","合同id不能为空");
        }
        return mffllawyerletterservice.confirm(id);
    }



    @GetMapping(value = "/{id}")
    @ApiOperation(value = "按id获取律师函")
    public ReturnBean<MfflLawyerletterVo> get(@ApiParam(name = "id", value = "律师函id", required = true) @PathVariable Integer id) {
        if (id == null) {
            return new ReturnBean("201", "律师函id不能为空");
        }
        return mffllawyerletterservice.getIdlawyerletter(id);
    }


    /*@PostMapping
    @ApiOperation(value = "保存")
    public MfflLawyerletter save(@RequestBody MfflLawyerletter mfflLawyerletter) {
        mfflLawyerletterDao.save(mfflLawyerletter);

        return mfflLawyerletter;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public MfflLawyerletter get(@PathVariable Long id) {
        return mfflLawyerletterDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public MfflLawyerletter update(@RequestBody MfflLawyerletter mfflLawyerletter) {
        mfflLawyerletterDao.update(mfflLawyerletter);

        return mfflLawyerletter;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflLawyerletterDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflLawyerletter> list(PageTableRequest request) {
                return mfflLawyerletterDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mfflLawyerletterDao.delete(id);
    }*/
}
